// pages/wallet/withdraw/index.js
const app = getApp() // 引入app
import _request from '../../../utils/request.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl, //全局img路径
    accountObj: null,
    money: '',
    cashInfoList: [],
    bank: null,
    walInfo: null
  },

  // 提现全部金额
  getAll() {
    this.setData({
      money: this.data.accountObj.balance
    })
  },

  // 提现金额
  getinput(e) {
    let value = e.detail.value
    if (e.detail.value.substr(0, 1) == 0 && value.length == 1) {
      value = value.replace(/[^0-9-]+/, '');
      if (value < 1) {
        value = 1
      }
      this.setData({
        money: value
      })
    } else {
      this.setData({
        money: e.detail.value
      })
    }
  },

  // 提现明细
  onDetails() {
    wx.navigateTo({
      url: '/pages/wallet/txdetails/index',
    })
  },

  // 过滤
  validateNumber(val) {
    return val.replace(/\D/g, '')
  },

  // 添加银行卡
  addCard() {
    wx.navigateTo({
      url: '/pages/wallet/bankcardmanagement/index',
    })
  },

  // 提现
  onWith() {
    // 钱包提现
    this.getRule() && _request.postObj('/wallet/toCash', {
      userId: wx.getStorageSync('userid'),
      accountId: this.data.bank.id,
      money: this.data.money,
      toCashVersion: 1,
    }).then((res) => {
      this.onCommont()
      this.setData({
        money: ''
      })
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  //验证提现数额 最大两万，最小100
  getRule() {
    const {
      walInfo,
      money
    } = this.data
    const {
      maxWithdrawal,
      minWithdrawal
    } = walInfo
    console.log(this.data);
    if (maxWithdrawal > money > minWithdrawal && this.data.bank) {
      return true
    } else if ((Number(maxWithdrawal) < Number(money)) && this.data.bank) {
      wx.showToast({
        title: '提现金额不能大于' + maxWithdrawal,
        icon: 'none'
      })
      return false
    } else if ((minWithdrawal > money)  && this.data.bank) {
      wx.showToast({
        title: '提现金额不能小于' + minWithdrawal,
        icon: 'none'
      })
      return false
    } else if (!this.data.bank) {
      wx.showToast({
        title: '请选择银行卡',
        icon: 'none'
      })
      return false
    } else {
      return true
    }
  },

  onCommont() {
    // 获取余额 
    _request.get('/wallet/getCashInfo', {
      id: wx.getStorageSync('userid'),
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      this.setData({
        accountObj: res.data.data
      })
    }).catch((error) => {
      console.log(error, 'error');
    })

    // 用户钱包提现明细
    _request.get('/wallet/toCashInfoList', {
      date: '',
      pageNo: 1,
      pageSize: 10,
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      this.setData({
        cashInfoList: res.data.data
      })
    }).catch((error) => {
      console.log(error, 'error');
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 提现信息 大小限制
    _request.get('/wallet/getWithdrawalInfo', {}).then((res) => {
      this.setData({
        walInfo: res.data.data
      })
    }).catch((error) => {
      console.log(error, 'error');
    })
    this.onCommont()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})